package com.softstao.guoyu.ui.adapter;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.softstao.guoyu.R;
import com.softstao.guoyu.model.sale.FromOrder;
import com.softstao.guoyu.model.sale.OrderGoods;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewBaseAdapter;
import com.softstao.guoyu.ui.baseAdapter.RecycleViewHolder;
import com.softstao.softstaolibrary.library.utils.LZUtils;
import com.softstao.softstaolibrary.library.widget.FullyLinearLayoutManager;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by vaikus on 2017/6/16.
 */

public class FromOrderAdapter extends RecycleViewBaseAdapter<FromOrder> {
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    private AdapterItemListener adapterItemListener;

    public FromOrderAdapter(List<FromOrder> datas) {
        super(datas, R.layout.layout_order_list_item);
    }

    @Override
    public void convert(RecycleViewHolder holder, FromOrder order) {
        RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        holder.itemView.setLayoutParams(layoutParams);
        holder.getView(R.id.source_sn).setVisibility(View.VISIBLE);
        holder.getView(R.id.addr_name).setVisibility(View.VISIBLE);
        if (order.getIsPay()==1){
            holder.getView(R.id.pay_way).setVisibility(View.VISIBLE);
        }else {
            holder.getView(R.id.pay_way).setVisibility(View.GONE);
        }
        holder.setText(R.id.order_sn, "订单编号：" + order.getOrderCode())
                .setText(R.id.time,"时间：" + format.format(new Date(order.getOrderDate())))
                .setText(R.id.from_agent, "下级代理：" + order.getFormAgentName())
                .setText(R.id.total_price, mContext.getResources().getString(R.string.rmb) + LZUtils.priceFormat(order.getTotal()))
                .setText(R.id.source_sn, "源订单号：" + order.getSourceCode())
                .setText(R.id.addr_name, "收货人：" + order.getAddrName())
                .setText(R.id.pay_way, "支付方式：" + order.getPayWay());
        holder.getView(R.id.contact).setOnClickListener(v -> {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            Uri data = Uri.parse("tel:" + order.getFormAgentMobile());
            intent.setData(data);
            mContext.startActivity(intent);
        });
        RecyclerView recyclerView = holder.getView(R.id.list_view);
        OrderGoodsAdapter adapter;
        if (order.getOrderProductList()==null){
            adapter = new OrderGoodsAdapter(new ArrayList<OrderGoods>());
        }else {
            adapter = new OrderGoodsAdapter(order.getOrderProductList());
        }
        recyclerView.setTag(adapter);
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new FullyLinearLayoutManager(holder.itemView.getContext()));
        adapter.setListener(position -> {
            if(adapterItemListener!=null){
                adapterItemListener.onItemClick(holder.getAdapterPosition(),position);
            }
        });
        changeStatus(holder,order);
    }

    public interface AdapterItemListener{
        void onCancel(int position);
        void onItemClick(int mPosition, int position);
        void onConfirm(int position);
        void onConfirmIncome(int position);
        void onTransferIncome(int position);
        void onRejectIncome(int position);
        void onShipping(int position);
    }

    public AdapterItemListener getAdapterItemListener() {
        return adapterItemListener;
    }

    public void setAdapterItemListener(AdapterItemListener adapterItemListener) {
        this.adapterItemListener = adapterItemListener;
    }

    private void changeStatus(final RecycleViewHolder holder, FromOrder order) {
        TextView btn1 = holder.getView(R.id.btn1);
        TextView btn2 = holder.getView(R.id.btn2);
        TextView btn3 = holder.getView(R.id.btn3);
        TextView btn4 = holder.getView(R.id.btn4);
        TextView status = holder.getView(R.id.status);
        LinearLayout bottom = holder.getView(R.id.bottom);
        bottom.setVisibility(View.VISIBLE);
        switch (order.getState()) {
            case FromOrder.UNCONFIRM:
                status.setText(order.getStateStr());
                btn1.setVisibility(View.VISIBLE);
                btn1.setText("确认订单");
                btn1.setOnClickListener(v -> {
                    if (adapterItemListener!=null){
                        adapterItemListener.onConfirm(holder.getAdapterPosition());
                    }
                });
                btn2.setVisibility(View.VISIBLE);
                btn2.setText("取消订单");
                btn2.setOnClickListener(v -> {
                    if (adapterItemListener!=null){
                        adapterItemListener.onCancel(holder.getAdapterPosition());
                    }
                });
                btn3.setVisibility(View.GONE);
                btn4.setVisibility(View.GONE);
                break;
            case FromOrder.UNPAY:
                status.setText(order.getStateStr());
                if (order.getIsPay()==0){
                    btn1.setVisibility(View.VISIBLE);
                    btn1.setText("取消订单");
                    btn1.setOnClickListener(v -> {
                        if (adapterItemListener!=null){
                            adapterItemListener.onCancel(holder.getAdapterPosition());
                        }
                    });
                    btn2.setVisibility(View.GONE);
                    btn3.setVisibility(View.GONE);
                    btn4.setVisibility(View.GONE);
                }else if (order.getIsPay()==1){
                    btn1.setVisibility(View.GONE);
                    btn1.setText("确认并发货");
                    btn1.setOnClickListener(v -> {
                        if (adapterItemListener!=null){
                            adapterItemListener.onConfirmIncome(holder.getAdapterPosition());
                        }
                    });
                    btn2.setVisibility(View.VISIBLE);
                    btn2.setText("确认并上移");
                    btn2.setOnClickListener(v -> {
                        if (adapterItemListener!=null){
                            adapterItemListener.onTransferIncome(holder.getAdapterPosition());
                        }
                    });
                    btn3.setVisibility(View.VISIBLE);
                    btn3.setText("驳回");
                    btn3.setOnClickListener(v -> {
                        if (adapterItemListener!=null){
                            adapterItemListener.onRejectIncome(holder.getAdapterPosition());
                        }
                    });
                    btn4.setVisibility(View.VISIBLE);
                    btn4.setText("取消订单");
                    btn4.setOnClickListener(v -> {
                        if (adapterItemListener!=null){
                            adapterItemListener.onCancel(holder.getAdapterPosition());
                        }
                    });
                }else if (order.getIsPay()==2){
                    btn1.setVisibility(View.VISIBLE);
                    btn1.setText("取消订单");
                    btn1.setOnClickListener(v -> {
                        if (adapterItemListener!=null){
                            adapterItemListener.onCancel(holder.getAdapterPosition());
                        }
                    });
                    btn2.setVisibility(View.GONE);
                    btn3.setVisibility(View.GONE);
                    btn4.setVisibility(View.GONE);
                }
                break;
            case FromOrder.PAYED:
                status.setText(order.getStateStr());
                btn1.setVisibility(View.VISIBLE);
                btn1.setText("填写快递信息");
                btn1.setOnClickListener(v -> {
                    if (adapterItemListener!=null){
                        adapterItemListener.onShipping(holder.getAdapterPosition());
                    }
                });
                btn2.setVisibility(View.VISIBLE);
                btn2.setText("取消订单");
                btn2.setOnClickListener(v -> {
                    if (adapterItemListener!=null){
                        adapterItemListener.onCancel(holder.getAdapterPosition());
                    }
                });
                btn3.setVisibility(View.GONE);
                btn4.setVisibility(View.GONE);
                break;
            case FromOrder.SHIPPED:
                status.setText(order.getStateStr());
                bottom.setVisibility(View.GONE);
                break;
            case FromOrder.COMPLETED:
                status.setText(order.getStateStr());
                bottom.setVisibility(View.GONE);
                break;
            case FromOrder.CANCEL:
                status.setText(order.getStateStr());
                bottom.setVisibility(View.GONE);
                break;
        }
    }
}
